package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Frame;
import java.awt.GridBagLayout;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JDialog;
import javax.swing.WindowConstants;

import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;



public class Dialog extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private static final Logger LOG = Logger.getLogger(Dialog.class.getName());
	
	
	public Dialog(final Frame parent, final String title) {
		super(parent, title, true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setFocusable(false);
		setLayout(new GridBagLayout());
	}
	
	
	
	protected void display() {
		pack();
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
	}
	
	@Override
	public final Frame getOwner() {
		return (Frame)super.getOwner();
	}
	
	protected void log(final Exception e) {
		if (e == null) {
			//	ignored
			return;
		} else if (e instanceof InvalidCurrentUserException) {
			log("The current user is not allowed to perform this action.",
					Level.WARNING);
		} else if (e instanceof RuntimeException) {
			log(e.getMessage(), Level.SEVERE);
		} else if (e instanceof TaskManagerException) {
			
			log(e.getMessage(), Level.WARNING);
		} else {
			LOG.log(Level.SEVERE, "", e);
			LOG.warning("    ERROR:  " + e.getMessage());
		}
		
		e.printStackTrace();
	}
	
	protected void log(final String msg) {
		LOG.info(msg);
	}
	
	protected void log(final String msg, final Level lvl) {
		LOG.log(lvl, msg);
	}
	
	protected void log(final String format, final Object... args) {
		log(String.format(format, args));
	}
	
}